﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Top_Meeting.UI;

using TopVisionVideoStream;

namespace Top_Meeting.Window
{
    public partial class Frm_Calling : Top_Meeting.Window.Frm_Base
    {
        #region Singleton

        public static Frm_Calling instance = null;

        #endregion

        #region FIELD        

        private string callid;

        #endregion


        #region Initialize        

        public Frm_Calling()
        {
            if (instance == null)
            {
                instance = this;
            }
            else if (instance != this)
            {
                Close();
                return;
            }

            InitializeComponent();

            this.callid = Configuration.CallingID;

            FrmInit();

            this.ShowInTaskbar = false;
        }

        #endregion


        #region OVERRIDE

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            VideoCall();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            instance = null;
        }

        protected override void UpdateLayout()
        {
            base.UpdateLayout();

            this.BackColor = TMColor.Color_Transparent;
            layPanel_Body.ImageToBackground("img_3018_n");

            //Robot
            layPanel_Robot.ImageToBackground("img_3027_n");
            layPanel_Wifi.ImageToBackground("img_3028_n");
            layPanel_Main_Robot.Align(1);

            //Button
            layBtn_Hangup.ImageToButtonImage("button_3015");
            layBtn_Hangup.Align(1);

            //ID
            layPanel_CallingID.ImageToBackground("img_3022_n");
            layLab_CallingID.Text = callid;

            //Tip
            layLab_CallingTip.Align(1);

            layLab_CallingTip.Text = "";

            ControBoxActive(false);
        }

        protected override void InitEvent()
        {
            base.InitEvent();

            layPanel_Main.MouseDown += Frm_MouseDown;
            layPanel_CallingID.MouseDown += Frm_MouseDown;
            layPanel_Main_Robot.MouseDown += Frm_MouseDown;
            layPanel_Robot.MouseDown += Frm_MouseDown;
            layPanel_Wifi.MouseDown += Frm_MouseDown;
            layLab_CallingID.MouseDown += Frm_MouseDown;
            layLab_CallingTip.MouseDown += Frm_MouseDown;

            layBtn_Hangup.Click += LayBtn_Hangup_Click;
        }

        #endregion


        #region PRIVATE_METHOD

        private void Hangup()
        {
            //SDK_Method

            Frm_Main.instance.ConferenceReady();

            Close();
        }

        public void ShowTip(string _msg)
        {
            layLab_CallingTip.Text = _msg;
        }

        #endregion


        #region CONTROL_EVENT

        private void LayBtn_Hangup_Click(object sender, EventArgs e)
        {
            VideoCallCancel();
        }

        #endregion


        #region Test

        #endregion


        #region CLIENT


        #region PRIVATE_METHOD

        private void VideoCall()
        {
            TVVS.VideoCall(callid, CallType.Video, VideoCallResult);
        }

        private void VideoCallCancel()
        {
            layBtn_Hangup.Enabled = false;
            TVVS.VideoCallCancel(null);
            Hangup();
        }

        private void VideoConnected()
        {
            Frm_Main.instance.VideoConnected();
            Close();
        }

        private async void VideoCallCancelDelay(int _msecond)
        {
            if (this.IsDisposed)
                return;

            layBtn_Hangup.Enabled = false;
            TVVS.VideoCallCancel(null);
            await Task.Delay(_msecond);
            Hangup();
        }

        #endregion


        #region CALLBACK

        public void VideoCallResult(CallStatusType e)
        {

            switch (e)
            {
                case CallStatusType.Failed:
                    ShowTip(Configuration.TipCalling_Failed);
                    break;
                case CallStatusType.Checking:
                    break;
                case CallStatusType.None:
                    ShowTip(Configuration.TipCalling_NotExist);
                    break;
                case CallStatusType.Offline:
                    ShowTip(Configuration.TipCalling_OffLine);
                    break;
                case CallStatusType.Online:
                    break;
                case CallStatusType.Calling:
                    ShowTip(Configuration.TipCalling_Calling);
                    break;
                case CallStatusType.Connected:
                    VideoConnected();
                    break;
                case CallStatusType.Busy:
                    ShowTip(Configuration.TipCalling_Busy);
                    break;
                case CallStatusType.DisConnect:
                    ShowTip(Configuration.TipCalling_Disconnect);
                    VideoCallCancel();
                    break;
                case CallStatusType.NoAnswer:
                    ShowTip(Configuration.TipCalling_NoAnswer);
                    break;
                case CallStatusType.StartCalling:
                    break;
                case CallStatusType.Hangup:
                    ShowTip(Configuration.TipCalling_Disconnect);
                    VideoCallCancelDelay(2000);
                    break;
                default:
                    break;
            }
        }

        #endregion


        #endregion

    }
}
